﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data.OleDb;
using System.IO;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using System.Configuration;

namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class getToken : IHttpHandler
    {
        public static readonly string bz = ConfigurationManager.ConnectionStrings["conn1"].ConnectionString;

        public void ProcessRequest(HttpContext context)
        {

      
            byte[] postData = new byte[context.Request.InputStream.Length];
            context.Request.InputStream.Read(postData, 0, postData.Length);
            string postDataString = System.Text.Encoding.UTF8.GetString(postData);
            string jsonData = "";
            TokenResModel res = new TokenResModel();
            res.msg = "";
            res.Token = "";
            try
            {
                
                PetaPoco.Database dbw = new PetaPoco.Database(bz);
                UserModel model = JsonConvert.DeserializeObject<UserModel>(postDataString);
                context.Response.ContentType = "application/json";
                string name = model.name;
                string pass = model.password;
                String sSubId = "AS";
                String sAccID =ConfigurationManager.AppSettings["ZTH"].ToString();
                String sYear = DateTime.Now.Year.ToString();
                String sDate =  DateTime.Now.ToString("yyyy-MM-dd");
                String sServer = ConfigurationManager.AppSettings["SERVER"].ToString();
                String sUserID = name;
                String sPassword = "";
                String sSerial = "";
                FileLogHelper.WriteTxtLog("调用开始", sUserID);
                FileLogHelper.WriteTxtLog("调用开始", sPassword);
                // 创建要返回的数据对象
                U8Login.clsLogin u8Login = new U8Login.clsLogin();
                if (!u8Login.Login(ref sSubId, ref sAccID, ref sYear, ref sUserID, ref sPassword, ref sDate, ref sServer, ref sSerial))
                {
                    res.msg = u8Login.ShareString;
                    res.Code = "999";
                  
                }
                else
                {
                    res.Code = "200";
                    res.Token = u8Login.userToken.Substring(u8Login.userToken.IndexOf("value") + 7, 60);
                    TokenModel token = new TokenModel();
                    token.token = u8Login.userToken.Substring(u8Login.userToken.IndexOf("value")+7,60);
                    token.name = model.name;
                    token.date = DateTime.Now;
                    FileLogHelper.WriteTxtLog("调用开始", "1");
                    TokenModel aa = dbw.FirstOrDefault<TokenModel>("select  *  from Token  where name='"+ model.name + "'");
                    if(aa==null)
                    {
                        FileLogHelper.WriteTxtLog("调用开始", "2");
                        dbw.Insert(token);
                    }
                    else
                    {
                        FileLogHelper.WriteTxtLog("调用开始", "3");
                        token.ID = aa.ID;
                        dbw.Update(token);
                    }  
                }
            }
            catch (Exception ee)
            {
                FileLogHelper.WriteTxtLog("调用开始", "999");
                res.msg = ee.Message;
                res.Code = "999";

                jsonData = JsonConvert.SerializeObject(res);
                context.Response.Write(jsonData);
                return;
            }

 
         
            // 写入响应

            jsonData = JsonConvert.SerializeObject(res);
            context.Response.Write(jsonData);
   
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string  res(string ccode,string msg,string jsonData)
        {
            string resstr = "";
            

            return resstr;
        }

        
    }
}